package com.plugin.dingding;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.PostClient;

import java.util.HashMap;
import java.util.Map;


public class ZWDingUtil {
	public static String appKey= "wm2023-4D1HS005xl041L6Q0aQF3ye";
	public static String appSecret = "Hn3OKsOC8CkPyGOLvOMEd5mmSlc0yua250upfPaz";
	public static String domain = "openplatform-pro.ding.zj.gov.cn";

	//### 测试服 ak sk
	public static String appKey_dev = "wm2023-4D1HS005xl041L6Q0aQF3ye";
	public static String appSecret_dev = "Hn3OKsOC8CkPyGOLvOMEd5mmSlc0yua250upfPaz";
	public static String domain_dev = "openplatform.dg-work.cn";
//	39.96.127.246
//	jx2023-gr0smXj67Lo2jSOyR3mQZFn
//	nz34eZLIFQ56n657CR9528AHWoKIqV8t48ZTxG7S
	public static String key = "";
	public static String token = "";

	public static ExecutableClient executableClient;

	public static void initZWD(boolean debugModel){


		if(debugModel){
			Clog.l("-----------ZWDing dev Model-----------");
			key = appKey_dev;
			token = appSecret_dev;
			domain = domain_dev;
		}else{
			Clog.l("-----------ZWDing pro Model-----------");
			key = appKey;
			token = appSecret;

		}
		executableClient =ExecutableClient.getInstance();
		executableClient.setAccessKey(key);
		executableClient.setSecretKey(token);
		executableClient.setDomainName(domain);
		executableClient.setProtocal("https");
		executableClient.init();
	}

	
	/**
	 * 
	 * {"success":true,"content":{"data":{"expiresIn":7200,"accessToken":"app_9947d32b96164810a85bf74647cb9e15"},"success":true,"requestId":"2760825515959825454692036e0962","responseMessage":"OK","responseCode":"0"}}

	 * @return
	 */
	public static String getToken() {
		String api ="/gettoken.json";
		PostClient postClient = executableClient.newPostClient(api);
		System.out.println(key);
		//设置参数
		postClient.addParameter("appkey", key);
		postClient.addParameter("appsecret",token);
		//调用API
		String apiResult = postClient.post();
		System.out.println(apiResult);
		JSONObject obj = JSONObject.parseObject(apiResult);
		JSONObject content = obj.getJSONObject("content");
		JSONObject data = content.getJSONObject("data");
		String accessToken  = data.getString("accessToken");
		System.out.println("token:::"+accessToken);
		return accessToken;
	}

	
	/**
	 * 
	 * accessToken
	 * 
	 * {"success":true,"content":{"data":{"expiresIn":7144,"accessToken":"jsApi_138b628583e949ee9105bd5124745200"},"success":true,"requestId":"2760828715959827217044902e7f53","responseMessage":"OK","responseCode":"0"}}

	 * @return
	 */
	public static String getTicket() {
		
		String api ="/get_jsapi_token.json";
		PostClient postClient = executableClient.newPostClient(api);
		//设置参数
		String token = getToken();
		postClient.addParameter("accessToken", token);
		//调用API
		String apiResult = postClient.post();
		System.out.println(apiResult);
		JSONObject obj = JSONObject.parseObject(apiResult);
		JSONObject content = obj.getJSONObject("content");
		JSONObject data = content.getJSONObject("data");
		String accessToken  = data.getString("accessToken");
		return accessToken;
	}
	
	/**
	 * 
	 * {"success":true,"content":
	 * {"data":
	 * {"lastName":"王明月","clientId":"blueshield2020",
	 * "realmId":53016,"openid":"86e94690cdcadf67426d7fd3351c5b78",
	 * "realmName":"湖州致胜电脑科技有限公司","nickNameCn":"王明月","tenantUserId":"53016$359296","employeeCode":"GE_9353962767bf4338b16656a34a369f49",
	 * "accountId":359296,"tenantName":"湖州致胜电脑科技有限公司","namespace":"local","tenantId":53016,"account":"csxzd-wmy"},"success":true,"responseMessage":"成功","responseCode":"0"}}

	 * @param code
	 * @return
	 */
	public static Map<String,Object> getDingUserByCode(String code) {
		String token = getToken();
		String api ="/rpc/oauth2/dingtalk_app_user.json";
		PostClient postClient = executableClient.newPostClient(api);
		//设置参数
		postClient.addParameter("access_token",token);
		postClient.addParameter("auth_code", code);
		//调用API
		String apiResult = postClient.post();
		System.out.println("apiResult---"+apiResult);
		JSONObject obj = JSONObject.parseObject(apiResult);
		JSONObject content = obj.getJSONObject("content");
		JSONObject data = content.getJSONObject("data");
		String openid = data.getString("openid");
		String tenantName = data.getString("tenantName");
		String lastName = data.getString("lastName");
		String nickNameCn = data.getString("nickNameCn");
		int tenantId = data.getIntValue("tenantId");
		int accountId = data.getIntValue("accountId");
		Map<String,Object> mapdata = new HashMap<String,Object>();
		mapdata.put("openid", openid);
		mapdata.put("tenantId", tenantId+"");
		mapdata.put("accountId", accountId+"");
		mapdata.put("tenantName", tenantName+"");
		mapdata.put("lastName", lastName+"");
		mapdata.put("nickNameCn", nickNameCn+"");
		return mapdata;
	}
	//{"_RequestId":"2760829415961647458864525e1a74","Message":"sp invoke err, service:com.alibaba.gov.cloud.nc.api.service.MessageService:1.0.0, method:notifyReceiverBoxMsgs, errMsg:\nerror message : [HSF-Provider-172.22.11.84] Error log: failed to convert map to pojo when generic invoke com.alibaba.gov.cloud.nc.api.service.MessageService:1.0.0#notifyReceiverBoxMsgs caused by For input string: \"53016$359296\"","success":false,"errorCode":"100","HostId":"openplatform.dg-work.cn","Code":"spInvokeErr","errorMsg":"sp invoke err, service:com.alibaba.gov.cloud.nc.api.service.MessageService:1.0.0, method:notifyReceiverBoxMsgs, errMsg:\nerror message : [HSF-Provider-172.22.11.84] Error log: failed to convert map to pojo when generic invoke com.alibaba.gov.cloud.nc.api.service.MessageService:1.0.0#notifyReceiverBoxMsgs caused by For input string: \"53016$359296\"","errorLevel":"error"}
	public static void sendMsg(String tid,String recid,String url,String title,String content,int typez) {
		System.out.println("==================>发送通知中==============");
		Map<String,Object> data = new HashMap<String,Object>();
		if(title==null)
			title="";
		if(content==null)
			content="";
		
		String api ="/message/workNotification";
		PostClient postClient = executableClient.newPostClient(api);
		
		
		//设置参数
		postClient.addParameter("organizationCodes", "");
		postClient.addParameter("receiverIds", recid);
		postClient.addParameter("tenantId", tid);
		postClient.addParameter("bizMsgId", "");
		String pic = "@lALOACZwe2Rk";
//		pic="";
		postClient.addParameter("msg", "{\"msgtype\": \"link\",\"link\": {\"messageUrl\": \""+url+"\",\"picUrl\":\""+pic+"\", \"title\": \""+title+"\",\"text\": \""+content+"\"} }");

		//调用API
		String apiResult = postClient.post();
		System.out.println(apiResult);
		
		
	}
	
	public static void main(String[] args) {
		initZWD(false);
		String token = getToken();
		Clog.l(token);
	}
}
